function e(e, t, a) {
  return t in e ? Object.defineProperty(e, t, {
    value: a,
    enumerable: !0,
    configurable: !0,
    writable: !0
  }) : e[t] = a, e;
}
let app = getApp();
// let compose = require('../../utils/compose');
var util = require('../../../utils/util.js');
var applet_park_api_url = app.globalData.applet_park_api_url;
var applet_park_api_url_imgUrl = app.globalData.applet_park_api_url_imgUrl;
const {
  requestDataparams
} = require('../../../utils/request.js');
function t(e) {
  return parseInt(e) < 10 && (e = "0" + e), e;
}


var a, s = require("../../../utils/calendar.js"), n = getApp();
Page({
  data: (a = {
    currenttab:'0',
    squareImg: '../../../images/duihao.png',
    orderlists:[],
    type:'',
    items:[],
    showModal: false,
    currentDate: '',
    currentWeek: '',
    beginTime: '',
    endTime: '',
    selectedId: [],
    isContine: 'true',
    checkIcon: false,
    roomPrice:0,
    totalPrice:0,
    openId:'',
    token:'',
  },e(a, "errorMessage", ""), e(a, "checkMessage", !1), e(a, "dialogDate", !1), e(a, "nowDateIndex", wx.getStorageSync("nowDateIndex")),
  e(a, "topDate", wx.getStorageSync("topDate")), e(a, "date_data1", []), e(a, "date_data2", []),
  e(a, "date_now", {
    month: "",
    year: "",
    value: ""
  }), e(a, "date_next", {
    month: "",
    year: "",
    value: ""
  }), e(a, "add_btn", !0), e(a, "reduce_btn", !1), e(a, "final_num", 1), e(a, "show_a", !1),
  e(a, "selecedList", []), e(a, "saleStatus", "nothing"), e(a, "saleContent", {}),
  e(a, "imgUrl", n.globalData.KrImgUrl), e(a, "cardStatus", "nothing"), e(a, "cardLength", 0),
  e(a, "discount", !1), e(a, "originalAmount", 0), a),
  
go: function() { 
  this.setData({
  showModal: false
  })
},
getNowRangeTime: function () {
  var e = this.data.detailInfo.meetingRoomId, t = this, a = [], n = [];
  wx.showLoading({
    title: "加载中"
  });
  var i = new Date(), s = 2 * i.getHours() + 1 + (i.getMinutes() > 29 ? 1 : 0), r = this.data.selectedTime;
  o.getRequest({
    url: o.globalData.KrUrl + "api/gateway/krmting/room/disableTime",
    methods: "GET",
    data: {
      date: t.data.orderDate.time,
      meetingRoomId: e
    },
    header: {
      "content-type": "appication/json"
    },
    success: function (e) {
      a = e.data.data.disableTime;
      for (var i = 19; i < 49; i++) {
        var o = {
          disabled: !1,
          number: i,
          actived: !1
        };
        n.push(o);
      }
      n.forEach(function (e, n) {
        a.indexOf(e.number) > -1 && (e.disabled = !0), "今天" == t.data.orderDate.timeText && e.number < s && (e.disabled = !0),
          r.indexOf(e.number) > -1 && (e.actived = !0);
      }), t.setData({
        rangeTime1: n.slice(0, 8),
        rangeTime2: n.slice(8, 16),
        rangeTime3: n.slice(16, 24),
        rangeTime4: n.slice(24),
        rangeTime: n
      }, function () {
        wx.hideLoading();
      });
    }
  });
},
getThemeName: function (e) {
  var t = e.time.split("-"), a = "" + t[1] + t[2] + "会议";
  this.setData({
    orderDate: e,
    themeName: a
  }), this.choose_date = e.time;
},
dealDateList: function () {
  var e = this.data.nowDate, t = this.data.topDate, a = "";
  t.map(function (t) {
    t.date === e && (a = t.type);
  });
  var n = this;
  n.james = new i.dateData({
    btn_bool: !0,
    data: t,
    init_data: {
      last_btn_num: e,
      last_data: a
    }
  }), this.setData({
    date_data1: n.james.date_data1,
    date_data2: n.james.date_data2
  });
},
dateBtn: function (e) {
  this.james.dateBtn(e);
  var t = this.james.getValue();
  this.setData({
    date_data1: this.date_data1,
    date_data2: this.date_data2,
    selecedList: t
  });
},
  onLoad: function () {
    var that = this;
    var openId = wx.getStorageSync("OPENID");
    var TOKEN = wx.getStorageSync("TOKEN");
    that.setData({
      openId:openId,
      token:TOKEN,
    })
    that.loadDatameetingorderlist();
    that.getNowFormatDate();
    

  },
  getNowFormatDate() {
    var that = this;
    var date = new Date();
    var seperator1 = "-";
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    var weekArr = ['星期天', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
    var week = weekArr[date.getDay()];
    if (month >= 1 && month <= 9) {
      month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
      strDate = "0" + strDate;
    }
    var currentdate = year + seperator1 + month + seperator1 + strDate;
    that.setData({
      currentDate: currentdate,
      currentWeek: week
    })

    return currentdate;
  },
  getTime(data,conferenceRoomId) {
    var that = this;
    
    wx.request({
      url: applet_park_api_url + '/api/conferenceRooms/scheduleTime',
      method: 'GET',
      data: {
        id: conferenceRoomId,
        day: data
      },
      success(res) {
        
        
        if (res.data.code == 0) {
          var timeList = res.data.data;
          var result = [];
          for (var i = 0; i < timeList.length; i += 8) {
            result.push(timeList.slice(i, i + 8));
          }
          that.setData({
            rangeTime: result,
            rowLength: result.length
          })
        } else {
          wx.showToast({
            title: '网络异常',
          })
        }


      },
      fail(res) {
        wx.showToast({
          title: res.data,
        })
      }
    })

  },
  tapTime: function (e) {//选择时间
    var that = this;
    var nowIdArray = [];
    var nowTimeArray = [];
    var checkId = e.currentTarget.dataset.index;
    var checkTime = e.currentTarget.dataset.time;
    var timeState = e.currentTarget.dataset.state;
    var checkIdArray = that.data.selectedId;//被选中时间的id
    var selectedTimeArray = that.data.selectedTime;
    //判断所选时间的id是否存在
    if (checkIdArray.length > 0) {
      nowIdArray = checkIdArray.concat(checkId);
      nowIdArray = nowIdArray.sort();
      var nowContinue = that.isContinuityNum(nowIdArray);//返回是否连续
      if (nowContinue == false) {
        //
        nowIdArray = nowIdArray.sort();
        if ((checkId == checkIdArray[0]) || (checkId == checkIdArray[checkIdArray.length - 1])) {
          //
          //删除
          var index = checkIdArray.indexOf(checkId);
          that.judgeIcon(checkId, false);
          checkIdArray.splice(index, 1);
          that.setData({
            selectedId: checkIdArray
          })
        } else {
          //
          nowIdArray = that.data.selectedId;
          wx: wx.showToast({
            title: '请选择连续的时间',
            icon: 'none',
            duration: 1000,
          })
        }
      } else {
        //添加
        that.judgeIcon(checkId, true);
        that.setData({
          selectedId: nowIdArray
        })
      }
    } else {
      that.judgeIcon(checkId, true);
      that.setData({
        selectedId: checkIdArray.concat(checkId),
      })
    }
    ////最终选中的所有时间id
    //处理时间
    var TimeList = that.data.rangeTime;
    for (var i = 0; i < that.data.selectedId.length; i++) {
      for (var j = 0; j < TimeList.length; j++) {
        var TimeItem = TimeList[j];
        for (var a = 0; a < TimeItem.length; a++) {

          if (that.data.selectedId[i] == TimeItem[a].timeId) {
            nowTimeArray = nowTimeArray.concat(TimeItem[a].time);
          }
        }
      }
    }
    ////最终选中的所有时间
    that.setData({
      selectedTime: nowTimeArray
    })
    //判断时间区间  先找出开始时间和结束时间
    that.handleTimeSection(that.data.selectedTime);

  },
  judgeIcon(checkId, iconState) {
    var that = this;
    var TimeList = that.data.rangeTime;
    for (var i = 0; i < TimeList.length; i++) {
      var TimeItem = TimeList[i];
      for (var j = 0; j < TimeItem.length; j++) {
        if (checkId == TimeItem[j].timeId) {
          TimeItem[j].activity = iconState;
        }
      }
    }
    ////最终icon显示效果
    that.setData({
      rangeTime: TimeList
    })

  },
  isContinuityNum(num) {//判断是否连续
    var that = this;
    let array = [];
    if (num instanceof Array) {
      array = [...num];
    } else {
      array = Array.from(num.toString())//转换为数组
    }

    var i = array[0];
    var isContinuation = true;
    for (var e in array) {
      if (array[e] != i) {
        isContinuation = false;
        break;
      }
      i++;
    }
    return isContinuation;
  },
  judgeRepeat(checkTime, selectedTimeArray) {//判断选中的日期是否存在
    var that = this;
    for (var i = 0; i < selectedTimeArray.length; i++) {
      var index = selectedTimeArray.indexOf(checkTime);
      if (index > -1) {
        selectedTimeArray.splice(index, 1);//存在就删除
      } else {
        that.setData({
          selectedTime: selectedTimeArray.concat(checkTime),
        })
      }
    }

  },
  handleTimeSection(section) {//先找出开始时间和结束时间
    var that = this;
    var maxTime = '';//最大的时间
    var minTime = '';//初始时间
    var newSection = [];
    for (var i = 0; i < section.length; i++) {
      newSection = newSection.concat(that.data.currentDate + " " + section[i]);
    }
    //sort按时间正序排序
    newSection.sort(function (a, b) {
      return Date.parse(b.time) - Date.parse(a.time);
    });
    maxTime = newSection[newSection.length - 1];
    minTime = newSection[0];
    
    
    //that.handleTime(maxTime, minTime)
    var val = maxTime;
    var sdate1 = new Date(val);
    sdate1.setMinutes(sdate1.getMinutes() + 30);
    var nowHours = sdate1.getHours() > 9 ? sdate1.getHours().toString() : '0' + sdate1.getHours();
    var nowMinutes = sdate1.getMinutes() > 9 ? sdate1.getMinutes().toString() : '0' + sdate1.getMinutes();
    var now = nowHours + ":" + nowMinutes;
    
    var endtime = that.data.currentDate + " " + now;
    
    that.setData({
      beginTime: minTime,
      endTime: endtime
    })
    that.TimeDifference(minTime,endtime);
   
  },
  handleTime(val, minTime) {
    
    var that = this;
    
  },
  TimeDifference(time1,time2){
    var that = this;
  var begin1=time1.substr(0,10).split("-");
  var end1=time2.substr(0,10).split("-");
  var date1=new Date(begin1[1] + - + begin1[2] + - + begin1[0]);
  var date2=new Date(end1[1] + - + end1[2] + - + end1[0]);
  var m=parseInt(Math.abs(date2-date1)/1000/60);
  var min1=parseInt(time1.substr(11,2))*60+parseInt(time1.substr(14,2));
  var min2=parseInt(time2.substr(11,2))*60+parseInt(time2.substr(14,2));
  var n=min2-min1;
  var minutes=m+n;
  var selectedTime = minutes/60;
  
  
  var totalPrice = parseFloat(selectedTime) * parseInt(that.data.roomPrice);
 
  that.setData({
    totalPrice : totalPrice
  })

  },
  judgeAction(){
    var that =this;
    var showStatus = that.data.showStatus;
    if(showStatus){//费用详情未打开
       that.actionShow()
    }else{
       that.actionHide();
    }
  },
  actionShow: function () {
    var that = this;
    that.setData({
      showStatus: false,
    });
  },

  packBp(e) {
    this.setData({
      currenttab: e.currentTarget.dataset.index
    })
    if (e.currentTarget.dataset.index == 0) {
      this.setData({
        type: ''
      })
    }
    if (e.currentTarget.dataset.index == 1) {
      this.setData({
        type: '1'
      })
    }
    if (e.currentTarget.dataset.index == 2) {
      this.setData({
        type: '2'
        
      })
    }

    this.loadDatameetingorderlist();
  },
  /**

  * 页面相关事件处理函数--监听用户下拉动作
  */

  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
    this.loadDatameetingorderlist();
    wx.showToast({
      title: '刷新成功',
      icon: 'success',
      duration: 2000
    })
  },
  enterAddserves(e){//进入增值服务页面 选择增值服务 直接在增值服务页面下单
    
    var conferenceRoomId = e.currentTarget.dataset.conferenceroomid;
    var conferenceOrderId = e.currentTarget.dataset.id;
    var pageName = 'meetingOrderList'
        wx.navigateTo({
          url: '../addServes/addServes?pageName='+pageName+'&totlaPrice='+0+'&conferenceRoomId='+conferenceRoomId+'&conferenceOrderId='+conferenceOrderId,
        })
  },
  contract(e){
    
    var that = this;
    var startTime = e.currentTarget.dataset.starttime;
    var chioceDate = startTime.substring(0,10)
    var endTime = e.currentTarget.dataset.endtime;
    var chioceId = e.currentTarget.dataset.chioceid;//
    var conferenceRoomId = e.currentTarget.dataset.conferenceroomid;//
    var roomPrice = e.currentTarget.dataset.roomprice;//会议室单价
    that.getTime(that.data.currentDate,conferenceRoomId);
    that.setData({
      showModal: true,
      roomPrice:roomPrice,
      conferenceOrderId:chioceId
      })
      
  },
  goPay(e){//直接支付
    var that = this;
     var totalAmount = e.currentTarget.dataset.totalamount;
     var totlaPrice = totalAmount + '00';
     var orderId = e.currentTarget.dataset.id;
     wx.request({
      url: applet_park_api_url + '/api/wechatPay/conferenceOrderListPay',
      method: 'POST',
      header: {
        Authorization:that.data.token,
        'Content-Type':"application/x-www-form-urlencoded",
      },
      data: {
        totalAmount: 1,
        orderId: orderId,
        openid: that.data.openId
      },
      success(res2) {
        
        if (res2.data.code == 0) {
          
          
          
          
          wx.requestPayment({
            timeStamp: res2.data.data.timeStamp,
            nonceStr: res2.data.data.nonceStr,
            package: res2.data.data.prepay_id,
            signType: 'MD5',
            paySign: res2.data.data.sign,
            paySource: 1,
            success(res) {

              wx.showToast({
                title: '支付成功',
              })
              setTimeout(function() {//刷新列表
                that.loadDatameetingorderlist();
              }, 500);
            },
            fail(res3) {
              
            }
          })

        } else {
          wx.showToast({
            title: '网络异常',
          })
        }
      },
      fail(res) {
        wx.showToast({
          title: res.data,
        })
      }
    })
  },
  submitPay() {//续约支付
    let that = this;
    
    var totlaPrice = that.data.totalPrice + '00';
    var beginTime = that.data.beginTime +':00';
    var endTime = that.data.endTime +':00';
    wx.request({
      url: applet_park_api_url + '/api/wechatPay/conferenceRenewal',
      method: 'POST',
      header: {
        Authorization:that.data.token,
        'Content-Type':"application/x-www-form-urlencoded",
      },
      data: {
        totalAmount: 1,
        conferenceOrderId: that.data.conferenceOrderId,//会议室ID
        startTime: beginTime,//预约开始时间
        endTime: endTime,//
        openid: that.data.openId
      },
      success(res2) {
        
        if (res2.data.code == 0) {
          
          
          
          
          wx.requestPayment({
            timeStamp: res2.data.data.timeStamp,
            nonceStr: res2.data.data.nonceStr,
            package: res2.data.data.prepay_id,
            signType: 'MD5',
            paySign: res2.data.data.sign,
            paySource: 1,
            success(res) {

              wx.showToast({
                title: '支付成功',
              })
              setTimeout(function() {//刷新列表
                that.loadDatameetingorderlist();
              }, 500);
            },
            fail(res3) {
              
            }
          })

        } else {
          wx.showToast({
            title: '网络异常',
          })
        }
      },
      fail(res) {
        wx.showToast({
          title: res.data,
        })
      }
    })
   
  },
  cancleOrder(){
    
  },
  handleTime(val) {
    var that = this;
    var val = val;
    var date = val.substring(0,10);
    var sdate1 = new Date(val);
    var now = sdate1.getTime()+1000*60*30;
    now = new Date(now)
    now = that.formatDate(now)
    return now;
  },
  formatDate(now) { 
    var date = now;
    date.getFullYear(); // 获取完整的年份(4位,1970)
    date.getMonth(); // 获取月份(0-11,0代表1月,用的时候记得加上1)
    date.getDate(); // 获取日(1-31)
    date.getTime(); // 获取时间(从1970.1.1开始的毫秒数)
    date.getHours(); // 获取小时数(0-23)
    date.getMinutes(); // 获取分钟数(0-59)
    date.getSeconds(); // 获取秒数(0-59)
    var date = new Date(now);
    Y = date.getFullYear() + '-';
    M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
    D = date.getDate() + ' ';
    h = date.getHours() + ':';
    m = date.getMinutes() < 10 ? '0'+date.getMinutes() : date.getMinutes();
    s = date.getSeconds(); 
    return Y+M+D+h+m; 
} ,
  loadDatameetingorderlist() {
    this.setData({
      title: '',
    })
    let that = this;
    that.data.items = [];
    that.setData({
      items: []
    })
    
    var url = applet_park_api_url_imgUrl;
    requestDataparams('/api/conferenceOrders', {type:that.data.type}, 'GET').then(res => {
      if (res.data.code == 0) {
        let items = [];
        var lists = res.data.data;
        
        for (var i=0;i<lists.length;i++) {
           if(lists[i].images){
               var listsImg = lists[i].images.split(',')[0];
               lists[i].images = url + listsImg;
           }

           if(lists[i].appointmentStartTime){
            lists[i].appointmentStartTime = lists[i].appointmentStartTime.substr(0,16);
           }
           //结束时间
           if(lists[i].renewalEndTime){
              lists[i].appointmentEndTime = lists[i].appointmentEndTime.substr(0,16);
           }else{
            if(lists[i].appointmentEndTime){
              lists[i].appointmentEndTime = lists[i].appointmentEndTime.substr(0,16);
             }
           }
           //价格
           if(lists[i].totalAmount){
            lists[i].totalAmount = parseInt(lists[i].totalAmount)/100;
           }
        }
        that.setData({
          items: lists
        })
      }
    }).catch(err => {
      
      that.setData({
        title: '暂无更多数据',
      })
    });
  },
  meetingDetail(e){
    
    var conferenceRoomId =e.currentTarget.dataset.conferenceroomid;
      wx.navigateTo({
        url: '../MeetingDetail/MeetingDetail?conferenceRoomId='+conferenceRoomId,
      })
  },
  
})